<template>
  <div>
    <article
        class="article article-type-post article-index blog-animation"
        v-for="(item,index) of arr" :key="item.articleid"
        :style="{'animation-duration':(index+1)*600+'ms'}"
    >

      <div class="article-inner">
        <router-link class="article-more-a article-title" :to="'/blogshow/'+item.articleid">
          <header class="article-header">
            <div style="margin-right:7.6923%">
              <h3 class="title">
                <span style="color: red">#</span> {{ item.articlename }}
              </h3>
              <span class="archive-article-date">
                <a href="javascritp:;" style="color:#bebebe;font-size:16px">
                  <i class="iconfont iconriqi"></i> {{ item.creatdate.split(" ")[0] }}
                </a>
              </span>
            </div>
          </header>
          <div class="article-entry" itemprop="articleBody" style="overflow:hidden">
            <el-image
                fit='cover'
                class="article-img"
                :src="item.imgpathid"
            >
              <div slot="error" class="image-slot">
                <img style="object-f:cover;width:100%;transform: translateY(-10%)" src="../../../assets/loading.gif"/>
              </div>
            </el-image>
            <div class="cont-desc" v-html="item.tips"></div>
          </div>
        </router-link>
        <div class="article-info article-info-index">
          <div class="article-group" style="display:flex">
            <div class="article-category tagcloud">
              <i class="iconfont iconshu icon">{{ item.firsttype }}</i>
            </div>
          </div>
          <div class="blog-info">
            <img @click="clickbest(item.articleid)"
                 :src="item.isbest?require('../../../assets/爱心.png'):require('../../../assets/爱心_noclick.png')"
                 style="height: 24px;width: 20px;float: left;cursor: pointer;margin-right: 5px"/><span
              style="color: #c7c4c4">   {{ item.bestcount }}</span>
          </div>
        </div>
      </div>
    </article>
  </div>
</template>
<script>
export default {
  name: "BlogList",
  props: {
    list: Array,
  },
  data: function () {
    return {
      arr: [],
      clicksrc: require("../../../assets/爱心_noclick.png")
    }
  },
  created() {
    let token = window.localStorage.getItem('myToken');
    if (token == null) {
      let arr = JSON.parse(JSON.stringify(this.list))
      arr.filter(async (item, i) => {
        await this.getimg(item.imgpathid).then(async res => {
          item.imgpathid = res
          await this.getBestCountWithArticleId(item.articleid).then(res => {
            item.bestcount = res
          })
        })
      })
      this.arr = arr
    } else {
      let arr = JSON.parse(JSON.stringify(this.list))
      arr.filter(async (item, i) => {
        await this.getimg(item.imgpathid).then(async res => {
          item.imgpathid = res
          await this.getBestCountWithArticleId(item.articleid).then(res => {
            item.bestcount = res
          })
          await this.checkWeatherIsSelected(item.articleid).then(res => {
            item.isbest = res
          })
        })
      })
      this.arr = arr.reverse()
    }

  },
  methods: {
    clickbest(articleid) {
      let token = window.localStorage.getItem('myToken');
      if (token == null) {
        this.$message.error("请登录后点赞！！")
        return
      }
      this.$http.put("/article/likeOrCancel/" + articleid).then(res => {
        if (res.code == 200) {
          let arr = JSON.parse(JSON.stringify(this.list))
          arr.filter(async (item, i) => {
            await this.getimg(item.imgpathid).then(async res => {
              item.imgpathid = res
              await this.getBestCountWithArticleId(item.articleid).then(res => {
                item.bestcount = res
              })
              await this.checkWeatherIsSelected(item.articleid).then(res => {
                item.isbest = res
              })
            })
          })
          this.arr = arr.reverse()
        }
      })
    },
    checkWeatherIsSelected(articleid) {
      return new Promise(resolve => {
        this.$http.get("/article/checkWeatherIsSelected/" + articleid).then(data => {
          resolve(data.data)
        })
      })
    },
    getBestCountWithArticleId(articleid) {
      return new Promise(resolve => {
        this.$http.get("/article/getBestCountWithArticleId/" + articleid).then(data => {
          resolve(data.data)
        })
      })
    },
    getimg(id) {
      return new Promise(resolve => {
        this.$http({
          url: "/file/img/" + id,
          method: "get",
          responseType: "arraybuffer",
        }).then(res => {
          let bytes = new Uint8Array(res);
          let storeData = "";
          let len = bytes.byteLength;
          for (let i = 0; i < len; i++) {
            storeData += String.fromCharCode(bytes[i]);
          }
          let database = "data:image/png;base64," + window.btoa(storeData);
          resolve(database);
        });
      })
    },
  }
};
</script>

<style lang="stylus" scoped>
.aixin {
  font-family: ngcs8-xsz3m;
}

.article {
  margin: 10px;
  position: relative;
  border: 1px solid #ddd;
  border-top: 1px solid #fff;
  border-bottom: 1px solid #fff;
  background: #fff;
  transition: all 0.2s ease-in;
  border-radius: 4px;

  &:hover {
    box-shadow: 8px 14px 38px rgba(231, 238, 239, 0.81);
    transition: transform .5s
    color #34495e

    .article-header {
      border-left 5px solid var(--main-6)
    }

    .el-icon-thumb {
      color: var(--main-5)
    }

    >>> .el-image__inner {
      transform scale(1.1)
    }
  }

  >>> .el-image__inner {
    transition: transform .5s
  }
}

@media (max-width: 996px) {
  .article-header {
    position relative
  }

  .article-img {
    float none !important
    display block
    width: auto !important;
    height: auto !important;
  }

  .archive-article-date {
    position: absolute;
    transform: translateY(50%);
    bottom: 0;
    right: 7.7%;
  }

  .cont-desc {
    margin: 0 10px !important
  }
}

.blog-info a {
  margin-right: 10px;
  color: #b4b4b4;
  transition: .1s ease-in;
  font-size: 14px;
  font-weight: 400;
  line-height: 20px;
  transition all .2s

  &:hover {
    color: var(--main-6)

    i {
      color: var(--main-6)
    }
  }

}

.blog-info i {
  color #bebebe
  margin-right: 1px
}

// .article-inner span {
//   font-size: 1.75rem;
//   line-height: 1.25;
//   margin-top: 2em;
//   margin-bottom: 1em;
// }
.iconfont {
  speak: none;
  font-style: normal;
  font-weight: 400;
  font-variant: normal;
  text-transform: none;
  line-height: 1;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

.article-inner h1.article-title, .article-title {
  color: #333;
  display: inherit;
  font-size: 18px;
  font-weight: 700;
  line-height: 1.5
}

.article-group {
  display flex
  flex-wrap: wrap

}

.article-category .icon, .article-tag .icon {
  color: #bebebe;
  float: left;
  margin-right: 10px;
  margin-top: 6px;
  overflow hidden
}

.article-tag-list {
  overflow hidden
}

.article-tag-list .article-tag-list-item {
  float: left;
}

.article-inner {
  border-color: #d1d1d1;
}

.article-more-a:visited {
  color: #969696;
}

.article-header {
  transition all .5s
  border-left: 5px solid #dddcdc;
  padding: 30px 0 15px 25px;
  padding-left: 7.6923%;
  border-radius: 4px;

}

.article-header .title {
  font-size: 16px
  display: inline;
  margin 0

}

.article-header .title:before {
  content: ''
}

.article-entry {
  line-height: 1.8em;
  margin-right: 7.6923%;
  margin-left: 7.6923%;
  border-top: 1px dashed #ddd
}

.article-entry .el-image {
  width: 300px;
  height: 180px;
  vertical-align: text-top;
  margin: 0 10px;
  float: left;
  border-radius: 4px;
}

.article-entry {
  padding 10px 0
}

.article-entry .cont-desc, p {
  margin: -6px 10px
  font-size: 13px;
  line-height: 24px;
  color: #bebebe
  cursor pointer
}

.article-info-index.article-info {
  display flex
  justify-content space-between
  flex-wrap: wrap
  align-items: center;
  margin: 0px 7.6923% 0;
  min-height: 72px;
  border-top: 1px solid #ddd;
}

// .article-inner span:first-child {
//   margin-bottom: 10px;
//   display: inline;
// }

.archive-article-date {
  color: #bebebe;
  float: right;
}

.tagcloud a.color3 {
  background: #bebebe;

  &:hover {
    color: var(--main-6)
  }
}

.tagcloud a {
  display: inline-block;
  text-decoration: none;
  font-weight: 400;
  font-size: 10px;
  color: #fff;
  height: 18px;
  line-height: 18px;
  float: left;
  padding: 0 5px 0 10px;
  position: relative;
  border-radius: 0 5px 5px 0;
  margin: 5px 9px 5px 8px;
  font-family: Menlo, Monaco, Andale Mono, lucida console, Courier New, monospace;
}

.tagcloud a.color3:before {
  border-right-color: #bebebe;
}

.tagcloud a:before {
  content: ' ';
  width: 0;
  height: 0;
  position: absolute;
  top: 0;
  left: -18px;
  border: 9px solid transparent;
}

.tagcloud a:after {
  content: ' ';
  width: 4px;
  height: 4px;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.3);
  position: absolute;
  top: 7px;
  left: 2px;
}

.article-more-link a {
  background: #4d4d4d;
  color: #fff;
  font-size: 12px;
  padding: 5px 8px;
  line-height: 16px;
  border-radius: 2px;
  transition: background 0.3s;
}

// .article-inner p {
//   margin: 1.6em 0;
// }

.article-more-link {
  margin-top: 0;
  text-align: left;
  float: right;
}
</style>